package server.container;

import server.*;

import java.io.IOException;
import java.util.List;
import java.util.Objects;

public class Engine implements Valve, Lifecycle {

    private List<Host> hostList;

    public List<Host> getHostList() {
        return hostList;
    }

    public void setHostList(List<Host> hostList) {
        this.hostList = hostList;
    }

    @Override
    public void invoke(Request request, Response response) {
        Host host = request.getHost();
        if (Objects.nonNull(host)){
            host.invoke(request,response);
        }else {
            try {
                response.output(HttpProtocolUtil.getHttpHeader404());
            } catch (IOException e) {
                e.printStackTrace();
            }

        }
    }

    @Override
    public void start() {
        hostList.forEach(Host::start);
    }
}
